<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Resurat extends CI_Controller {	
	
	public function index()
	{
		if(!$this->session->userdata('name')):
			redirect('auth');
		endif;
		
		$var['title']	= 'Opinion Surat';
		$no = $this->session->userdata('no_resurat');
		$kl = $this->session->userdata('klien_resurat');
		$ta = $this->session->userdata('tgl_resurat_awal');
		$tk = $this->session->userdata('tgl_resurat_akhir');
		$var['surats']	= $this->resurats->getData($no,$kl,$ta,$tk);
		$var['content'] = 'index-resurat';
		$this->load->view('index',$var);
	}
	
	function action(){
		if($this->session->userdata('no_resurat')==''):
			$surat=array('no_resurat'=>$this->input->post('no'),'klien_resurat'=>$this->input->post('s'));
		else:
			$surat=array('no_resurat'=>'','klien_resurat'=>'');
		endif;
		$this->session->set_userdata($surat);
		redirect($this->input->server('HTTP_REFERER'),301);
	}
	
	function date(){
		$start_date = $this->input->post('yyyy').'-'.$this->input->post('mm').'-'.$this->input->post('dd');
		$end_date   = $this->input->post('yyyy2').'-'.$this->input->post('mm2').'-'.$this->input->post('dd2');
		if($this->session->userdata('tgl_resurat_awal')==''):
			$surat=array('tgl_resurat_awal'=>$start_date,'tgl_resurat_akhir'=>$end_date);
		else:
			$surat=array('tgl_resurat_awal'=>'','tgl_resurat_akhir'=>'');
		endif;
		$this->session->set_userdata($surat);
		redirect($this->input->server('HTTP_REFERER'),301);
	}
	
	function excel(){
		$this->db->select('no_re,tanggal,kap,nama_klien,alamat,keterangan,pembuat');
		$this->db->order_by('tanggal','DESC');
	    $query = $this->db->get('rev_opinions');
        to_excel($query,'OP'); 
	}
	
	public function add_re_group()
	{
		
		$var['title']	= 'Add Review Opinion Surat';
		$var['no']		= $this->resurats->getLatestPK('no');
		$var['content'] = 'add-re-group';
		$this->load->view('index',$var);
	}
	
	public function add()
	{
		if(!$this->input->post('kap')):
			redirect('resurat/addgroup/');
		endif;
		
		if($this->input->post('kap')=='Kuningan Jakarta'):
			$cek = '01';
		elseif($this->input->post('kap')=='Sudirman Jakarta'):
			$cek = '02';
		else:
			$cek = '03';
		endif;
		
		$access = array('kap'=>$this->input->post('kap'),'code'=>$cek);
		$this->session->set_userdata($access);
		
		$var['title']	= 'Add  Review Opinion Surat';
		$var['kap']		= $this->session->userdata('kap');
		$var['code']	= $this->session->userdata('code');	
		$var['no']		= $this->resurats->getLatestPK('no');
		$var['content'] = 'add-resurat';
		$this->load->view('index',$var);
	}
	
	function save(){
		if($this->input->post('nama_klien')):
			$this->resurats->getSave();
		endif;
		redirect('resurat/index/',301);
	}
	
	public function edit($id)
	{
		$var['title']	= 'Edit Review Opinion Surat';
		$row            = $this->resurats->getID($id);
		$var['quo']		= $row; 
		$var['no']		= $this->resurats->getLatestPK('no');
		$var['content'] = 'edit-resurat';
		$this->load->view('index',$var);
	}
	
	function update(){
		if($this->input->post('keterangan')):
			$this->resurats->getUpdate();
		endif;
		redirect('resurat/index/',301);
	}
	
	function remove($id){
		if($id):
			$this->resurats->getRemove($id);
		endif;
		redirect($this->input->server('HTTP_REFERER'),301);
	}
	
	
	
	
}
